<?php
/**
  * wechat php 
  */
 function path_weixin($return_url){
	$sql = " SELECT * FROM ".$GLOBALS['ecs'] -> table('weixin_config')." WHERE id = 1";
	$wei = $GLOBALS['db'] -> getRow($sql);
	if(!isset($_GET['code'])){
		$encode_uri = urlencode($return_url);
		$f_url ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=$wei[appid]&redirect_uri=$encode_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
		header("location:$f_url");
	}
	
	if(isset($_GET['code'])){
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wei[appid]&secret=$wei[appsecret]&code=$_GET[code]&grant_type=authorization_code";
		$res = request($url);
		$_res = json_decode($res);
		$openid = $_res->openid;
		//获取用户基本信息
		$url_2 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$wei[appid]&secret=$wei[appsecret]";
		$res_2 = request($url_2);
		$_res_2 = json_decode($res_2);
		$access_token = $_res_2->access_token;
		$url_3 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
		$res_3 = request($url_3);
		$_res_3 = json_decode($res_3);

		if($openid){
			$_SESSION['openid'] = empty($openid) ? '' : $openid;
			$_SESSION['wei_info'] = empty($_res_3) ? '' : $_res_3;
		}

		header("location:$_SESSION[redirect_url]");
	}
	exit();
}

function get_share($return_url,$user_id){
	$data = array();
	$sql = " SELECT * FROM ".$GLOBALS['ecs'] -> table('weixin_config')." WHERE id = 1";
	$wei = $GLOBALS['db'] -> getRow($sql);
	if(!isset($_GET['code'])){
		$encode_uri = urlencode($return_url);
		$f_url ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=$wei[appid]&redirect_uri=$encode_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
		header("location:$f_url");
	}
	if(isset($_GET['code'])){
		//获取accesstoken
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$wei[appid]&secret=$wei[appsecret]";
		$res = request($url);
		$_res = json_decode($res);
		$access_token = $_res->access_token;

		//获取用户的推荐二维码
		$url_2 = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
		$data = array(
			'action_name' =>'QR_LIMIT_SCENE',
			'action_info' =>array(
									'scene'=>array(
										'scene_id'=>$user_id
									)
								)
		);

		$res_2 = request($url_2,json_encode($data));
		$_res_2 = json_decode($res_2);
		$ticket = $_res_2->ticket;
		
		if(!empty($ticket)){
			$sql = "UPDATE ".$GLOBALS['ecs']->table('users')." SET ticket = '$ticket' WHERE user_id = $user_id";
			$GLOBALS['db']->query($sql);
		}
		header("location:$_SESSION[redirect_url]");
	}
}


function wx_shouquan($return_url){
	$sql = " SELECT * FROM ".$GLOBALS['ecs'] -> table('weixin_config')." WHERE id = 1";
	$wei = $GLOBALS['db'] -> getRow($sql);
	if(!isset($_GET['code'])){
		$encode_uri = urlencode($return_url);
		$f_urls ="http://open.weixin.qq.com/connect/oauth2/authorize?appid=$wei[appid]&redirect_uri=$encode_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		header("location:$f_urls");
	}
	if (isset($_GET['code'])){
		$urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wei[appid]&secret=$wei[appsecret]&code=$_GET[code]&grant_type=authorization_code";
		$res = request($urls);
		$_res = json_decode($res);
		$refresh_token = $_res->refresh_token;
  	$url_4 = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$wei[appid]&grant_type=refresh_token&refresh_token=$refresh_token";
		$res_4 = request($url_4);
		$_res_4 = json_decode($res_4);
		$openids = $_res_4->openid;
		$access_tokens = $_res_4->access_token;
		$url_5 = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_tokens&openid=$openids&lang=zh_CN";
		$res_5 = request($url_5);
		$_res_5 = json_decode($res_5);
		if($openids){
			$_SESSION['openids'] = empty($openids) ? '' : $openids;
			$_SESSION['wei_infos'] = empty($_res_5) ? '' : $_res_5;
		}
		header("location:$_SESSION[redirect_url]");
	}else{
		ECHO'error';
	}
	
}


function request($url,$data='')
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		if(!empty($data)){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		}else{
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;  
			curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; 
		}
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		$res = curl_exec($ch);	
		return $res;
	}
	
?>